from flask import Flask,render_template,request, jsonify
from flask_cors import CORS
import HOME
import project_name

app=Flask(__name__)

# 允许所有来源
CORS(app)

# 首页
@app.route('/login',methods=['POST','GET'])
def login():
    if request.method == 'GET':
        try:
            top100_items,top10_people,act_proportion,activity_tend=HOME.inquire()
            data={
                "state":200,
                "top100_items":top100_items,
                "top10_people":top10_people,
                "act_proportion":act_proportion,
                "activity_tend":activity_tend
            }
            return jsonify(data)
        except Exception as e:
            print(str(e))
    else:
        data = {
            "state": 201
        }
        return jsonify(data)

# 接收请求项目名称
@app.route('/project',methods=['POST','GET'])
def project():
    if request.method == 'GET':
        item_name = request.args.get('item_name')
        try:
            act_proportion,activity_tend=project_name.inquire(item_name)
            data={
                "state":200,
                "act_proportion":act_proportion,
                "activity_tend":activity_tend
            }
            return jsonify(data)
        except Exception as e:
            print(str(e))
    else:
        data = {
            "state": 201
        }
        return jsonify(data)

@app.errorhandler(404)
def page_not_found(e):
    return  render_template("404.html")


if __name__=='__main__':
    # app.run(debug=True)
    app.run(host='0.0.0.0', port='5000', debug=True)


















